home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / Libraries / New WASTE Handlers / New 3D Handler / WEObject3D.c next >
Text File  |  1996-02-04  |  6KB  |  216 lines

  1. // ————• WASTE 3DMF Object—————————————————————————————————————————————————————————————
  2. // ———• by Chris K. Thomas, ckt@best.com
  3.  
  4. // ——• 5 Feb 96 ckt  1.0b1 - rev to current QD3D interface.
  5. // ——• 4 Jun 95 ckt  1.0a1 - created.
  6. // ——• Still need a way to set kViewerSize dynamically
  7.  
  8. // ————• Includes——————————————————————————————————————————————————————————————————————
  9.  
  10. #include "WEObject3D.h"        // ——• ourself
  11. #include "QD3DViewer.h"    // ——• QD3D viewer
  12.  
  13. // ————• Macros————————————————————————————————————————————————————————————————————————
  14.  
  15. #define ThrowIfOSErr_(x) if(x != noErr) goto Exit
  16.  
  17. // ————• Constants—————————————————————————————————————————————————————————————————————
  18.  
  19. static const Point    kViewerSize = {100, 100};
  20.  
  21. // ————• Instance Variables————————————————————————————————————————————————————————————
  22.  
  23. static CGrafPtr        sDefaultPort = NULL;
  24. // * if port is NULL, we just use the parent WASTE port
  25.  
  26. // ————• Static Prototypes—————————————————————————————————————————————————————————————
  27.  
  28. static pascal OSErr    HandleNew3D(Point *defaultObjectSize, WEObjectReference objectRef);
  29. static pascal OSErr    HandleDispose3D(WEObjectReference objectRef );
  30. static pascal OSErr    HandleDraw3D(Rect *destRect, WEObjectReference objectRef );
  31. static pascal Boolean HandleClick3D(Point hitPt, short modifiers,
  32.                                             long clickTime, WEObjectReference objectRef);
  33.  
  34. // ————• MixedMode——————————————————————————————————————————————————————————————————————
  35.  
  36. static UniversalProcPtr    sNew3DRD;
  37. static UniversalProcPtr    sDispose3DRD;
  38. static UniversalProcPtr    sDraw3DRD;
  39. static UniversalProcPtr    sClick3DRD;
  40.  
  41. // ————• Installer——————————————————————————————————————————————————————————————————————
  42.  
  43. OSErr WEObj3DInstall(WEHandle inWaste)
  44. {
  45.     OSErr theErr = noErr;
  46.     
  47.     // * mixed mode magic
  48.     
  49.     if(sNew3DRD == NULL)
  50.     {
  51.         sNew3DRD = NewWENewObjectProc(HandleNew3D);
  52.         sDispose3DRD = NewWEDisposeObjectProc(HandleDispose3D);
  53.         sDraw3DRD = NewWEDrawObjectProc(HandleDraw3D);
  54.         sClick3DRD = NewWEClickObjectProc(HandleClick3D);
  55.     }
  56.     
  57.     // * install
  58.     
  59.     theErr = WEInstallObjectHandler('3DMF', weNewHandler, sNew3DRD, inWaste);
  60.     ThrowIfOSErr_(theErr);
  61.     
  62.     theErr = WEInstallObjectHandler('3DMF', weDisposeHandler, sDispose3DRD, inWaste);
  63.     ThrowIfOSErr_(theErr);
  64.     
  65.     theErr = WEInstallObjectHandler('3DMF', weDrawHandler, sDraw3DRD, inWaste);
  66.     ThrowIfOSErr_(theErr);
  67.     
  68.     theErr = WEInstallObjectHandler('3DMF', weClickHandler, sClick3DRD, inWaste);
  69.     ThrowIfOSErr_(theErr);
  70.  
  71. Exit:    
  72.     return theErr;
  73. }
  74.  
  75. // ————• Setting info ———————————————————————————————————————————————————————————————————
  76. // ——• this is to be used when you're doing skanky things like
  77. // ——• changing WASTE's port to an offscreen one of your own only
  78. // ——• when it's drawing
  79. // ——• 
  80. // ——• (this could become a general utility for all WEObjs)
  81.  
  82. OSErr    WEObj3DSetDefaultPort(CGrafPtr inPort)
  83. {
  84.     sDefaultPort = inPort;
  85.     
  86.     return noErr;
  87. }
  88.  
  89.  
  90. // ————• New handler—————————————————————————————————————————————————————————————————————
  91. pascal OSErr    HandleNew3D(Point *outDefaultSize, WEObjectReference inObjectRef)
  92. {
  93.     OSErr            theErr = noErr;
  94.     Rect            viewFrame;
  95.     TQ3ViewerObject    curViewer;
  96.     Handle            data;
  97.     CGrafPtr        wastePort;
  98.     WEHandle        parentWaste;
  99.     
  100.     // * get incoming 3DMF data
  101.     data = WEGetObjectDataHandle(inObjectRef);
  102.     
  103.     HLockHi(data);
  104.     
  105.     // * use initial frame starting at origin
  106.     viewFrame.left = viewFrame.top = 0;
  107.     viewFrame.right = outDefaultSize->h = kViewerSize.h;
  108.     viewFrame.bottom = outDefaultSize->v = kViewerSize.v;
  109.     
  110.     // * Q3ViewerNew needs a port in which to draw
  111.     if(sDefaultPort == NULL)
  112.     {
  113.         // * use parent port
  114.         parentWaste = WEGetObjectOwner(inObjectRef);
  115.         theErr = WEGetInfo(wePort, &wastePort, parentWaste);
  116.         ThrowIfOSErr_(theErr);
  117.     }
  118.     else
  119.     {
  120.         // * use user-specified port
  121.         wastePort = sDefaultPort;
  122.     }
  123.     
  124.     // * create the viewer and store it for later reference
  125.     curViewer = Q3ViewerNew(wastePort, &viewFrame, kQ3ViewerDefault | kQ3ViewerDrawFrame);
  126.     WESetObjectRefCon(inObjectRef, (long)curViewer);
  127.     ThrowIfOSErr_(theErr);
  128.     
  129.     // * add data
  130.     theErr = Q3ViewerUseData (curViewer, *data, GetHandleSize(data));
  131.     
  132. Exit:
  133.     return theErr;
  134. }
  135.  
  136. // ————• Dispose handler—————————————————————————————————————————————————————————————————
  137. pascal OSErr    HandleDispose3D(WEObjectReference inObjectRef)
  138. {
  139.     OSErr            theErr = noErr;
  140.     TQ3ViewerObject    curViewer;
  141.     Handle            data;
  142.     
  143.     // * retrieve viewer
  144.     curViewer = (TQ3ViewerObject)WEGetObjectRefCon(inObjectRef);
  145.     
  146.     // * dispose viewer
  147.     theErr = Q3ViewerDispose(curViewer);
  148.     
  149.     // * dispose data
  150.     data = WEGetObjectDataHandle(inObjectRef);
  151.     DisposeHandle(data);
  152.     
  153.     return theErr;
  154. }
  155.  
  156. // ————• Draw handler————————————————————————————————————————————————————————————————————
  157. pascal OSErr    HandleDraw3D (Rect *inDestRect, WEObjectReference inObjectRef)
  158. {
  159.     OSErr            theErr = noErr;
  160.     WEHandle        parentWaste;
  161.     TQ3ViewerObject    curViewer;
  162.     CGrafPtr        wastePort, curPort;
  163.     
  164.     // * retrieve viewer & it's port
  165.     curViewer = (TQ3ViewerObject)WEGetObjectRefCon(inObjectRef);
  166.     curPort = Q3ViewerGetPort(curViewer);
  167.     
  168.     // * get parent port and draw using that
  169.     parentWaste = WEGetObjectOwner(inObjectRef);
  170.     theErr = WEGetInfo(wePort, &wastePort, parentWaste);
  171.     
  172.     if(wastePort != curPort)
  173.     {
  174.         theErr = Q3ViewerSetPort(curViewer, wastePort);
  175.         ThrowIfOSErr_(theErr);
  176.     }
  177.     
  178.     // * use new rectangle
  179.     theErr = Q3ViewerSetBounds(curViewer, inDestRect);
  180.     ThrowIfOSErr_(theErr);
  181.     
  182.     // * draw
  183.     theErr = Q3ViewerDraw(curViewer);
  184.     
  185.     // * restore port, if necessary
  186.     if(wastePort != curPort)
  187.     {
  188.         theErr = Q3ViewerSetPort(curViewer, curPort);
  189.         ThrowIfOSErr_(theErr);
  190.     }
  191.     
  192. Exit:
  193.     return theErr;
  194. }
  195.  
  196. // ————• Click handler———————————————————————————————————————————————————————————————————
  197.  
  198. pascal Boolean HandleClick3D(Point hitPt, short modifiers,
  199.                                         long /*clickTime*/, WEObjectReference inObjectRef)
  200. {
  201.     EventRecord        fake;
  202.     TQ3ViewerObject    curViewer;
  203.     
  204.     // * retrieve viewer
  205.     curViewer = (TQ3ViewerObject)WEGetObjectRefCon(inObjectRef);
  206.     
  207.     // * simulate click event
  208.     fake.where = hitPt;
  209.     LocalToGlobal(&fake.where);
  210.     fake.when = TickCount();
  211.     fake.what = mouseDown;
  212.     fake.modifiers = modifiers;
  213.     fake.message = 0;
  214.     
  215.     return (Q3ViewerEvent(curViewer, &fake));
  216. }